package com.frogs.ucenter.service.impl;

import java.util.Arrays;

import org.apache.commons.lang.ArrayUtils;
import org.jsoup.Connection.Method;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.frogs.system.util.ApiUrlParamEnum;
import com.frogs.system.web.api.entity.ApiUrl;
import com.frogs.system.web.api.response.Response.ApiResultEnum;
import com.frogs.system.web.api.response.Response.ResultParamEnum;
import com.frogs.ucenter.service.AbsService;
import com.frogs.ucenter.service.UserOpenPlatformService;

@Service("userOpenPlatformService")
public class UserOpenPlatformServiceImpl extends AbsService implements UserOpenPlatformService {
	
	private static Logger log = LoggerFactory.getLogger(UserOpenPlatformServiceImpl.class);
	
	@Override
	public JSONObject getUserInfo(String code, Integer openPlatformId, String openId, String ip) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_OPENPLATFORM_LOGIN");
		String[] otherParams = {
					ApiUrlParamEnum.code.getParamName(),code.toString(),
					ApiUrlParamEnum.openPlatformId.getParamName(),openPlatformId.toString(),
					ApiUrlParamEnum.openId.getParamName(),openId.toString(),
					ApiUrlParamEnum.ip.getParamName(), ip
				};
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			
			if (ApiResultEnum.SUCCESS.getId() != result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				throw new IllegalStateException(result.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
			return result;
		} catch (Exception e) {
			log.error("func[getUserInfo] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
	}

	@Override
	public JSONObject bindUserInfo(Integer openPlatformId, String openId,
			String userName, String password, String email, String phone,
			String code,Integer isphpw, String ip, Integer isRegister,String nickName,String expandCode) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_OPENPLATFORM_BIND_AND_LOGIN");
		String[] otherParams = {
					ApiUrlParamEnum.openPlatformId.getParamName(),openPlatformId.toString(),
					ApiUrlParamEnum.openId.getParamName(),openId.toString(),
					ApiUrlParamEnum.username.getParamName(),userName.toString(),
					ApiUrlParamEnum.password.getParamName(),password.toString(),
					ApiUrlParamEnum.email.getParamName(),email.toString(),
					ApiUrlParamEnum.phone.getParamName(),phone.toString(),
					ApiUrlParamEnum.code.getParamName(),code.toString(),
					ApiUrlParamEnum.isphpw.getParamName(),isphpw.toString(),
					ApiUrlParamEnum.ip.getParamName(), ip,
					ApiUrlParamEnum.isRegister.getParamName(), isRegister.toString(),
					ApiUrlParamEnum.nickName.getParamName(),nickName,
					ApiUrlParamEnum.expandCode.getParamName(),expandCode
				};
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			return result;
		} catch (Exception e) {
			log.error("func[bindUserInfo] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
	}
	
	@Override
	public boolean sendActivataAccountEmail(String openId, Integer openPlatformId, String email) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_OPEN_PLATFORM_ACTIVITE_EMAIL_SEND");
		String[] otherParams = {
				ApiUrlParamEnum.openId.getParamName(), openId,
				ApiUrlParamEnum.openPlatformId.getParamName(), openPlatformId.toString(),
				ApiUrlParamEnum.email.getParamName(), email
			};
		String[] allParams = (String[]) ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject) this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() == result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				return Boolean.TRUE;
		} catch (Exception e) {
			log.error("func[resetPasswd] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return Boolean.FALSE;
	}

}
